package com.company.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.project.entity.SysContentEntity;
import com.company.project.entity.SysDept;
import com.company.project.service.DeptService;
import com.company.project.service.SysContentService;
import com.company.project.vo.req.OrganMessageQueryReqVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.company.project.mapper.SysOrganMessageMapper;
import com.company.project.entity.SysOrganMessageEntity;
import com.company.project.service.SysOrganMessageService;
import org.springframework.util.StringUtils;

import java.util.Date;


@Service("sysOrganMessageService")
@Slf4j
public class SysOrganMessageServiceImpl extends ServiceImpl<SysOrganMessageMapper, SysOrganMessageEntity> implements SysOrganMessageService {

    @Autowired(required = false)
    private SysOrganMessageMapper sysOrganMessageMapper;
    @Autowired
    private DeptService deptService;
    @Autowired
    private SysContentService sysContentService;

    @Override
    public boolean saveNews(OrganMessageQueryReqVO sysContent) {
        SysDept sysDept = deptService.noeDeptInfo();
        // 记录新闻
        SysContentEntity sysContentEntity = saveContent(sysContent, sysDept);
        // 关联新闻
        SysOrganMessageEntity organMessageEntity = new SysOrganMessageEntity();
        organMessageEntity.setType(Integer.valueOf(sysContent.getType()));
        organMessageEntity.setContentId(sysContentEntity.getId());
        if(null != sysDept) {
            organMessageEntity.setDeptId(sysDept.getId());
        }else{
            organMessageEntity.setDeptId("0");
        }
        return this.save(organMessageEntity);
    }

    private SysContentEntity saveContent(OrganMessageQueryReqVO sysContent, SysDept sysDept) {
        // 记录文章信息
        SysContentEntity sysContentEntity = new SysContentEntity();
        if (StringUtils.isEmpty(sysDept) && StringUtils.isEmpty(sysContent.getTitle())) {
            sysContentEntity.setTitle("系统OrganType=" + sysContent.getType());
        } else if (StringUtils.isEmpty(sysContent.getTitle())) {
            sysContentEntity.setTitle(sysDept.getName() + "OrganType=" + sysContent.getType());
        } else if (!StringUtils.isEmpty(sysContent.getTitle())) {
            sysContentEntity.setTitle(sysContent.getTitle());
        }
        sysContentEntity.setContent(sysContent.getContent());
        sysContentEntity.setCreateTime(new Date());
        sysContentService.save(sysContentEntity);
        return sysContentEntity;
    }

    private boolean updateContent(OrganMessageQueryReqVO sysContent, SysDept sysDept){

        return true;
    }


    @Override
    public boolean updateNews(OrganMessageQueryReqVO sysContent) {
        return false;
    }

    @Override
    public IPage selectMyPage(OrganMessageQueryReqVO organMessageQueryReqVO) {
        Page page = new Page(organMessageQueryReqVO.getPage(), organMessageQueryReqVO.getLimit());
        QueryWrapper queryWrapper = new QueryWrapper();
//        String [] types = organMessageQueryReqVO.getType().split(",");
        queryWrapper.notIn("som.type", "0", "1","2");
//        queryWrapper.notIn("som.type","1");
        if (!StringUtils.isEmpty(organMessageQueryReqVO.getType())) {
            queryWrapper.in("som.type", organMessageQueryReqVO.getType().split(","));
        }
        if (!StringUtils.isEmpty(organMessageQueryReqVO.getDeptId())) {
            queryWrapper.eq("som.dept_id", organMessageQueryReqVO.getDeptId());
        }
        if (!StringUtils.isEmpty(organMessageQueryReqVO.getTitle())) {
            queryWrapper.like("sc.title", organMessageQueryReqVO.getTitle());
        }
        if (!StringUtils.isEmpty(organMessageQueryReqVO.getType())) {
            queryWrapper.eq("som.type", organMessageQueryReqVO.getType());
        }
        queryWrapper.orderByDesc("sc.create_time");
        log.info(queryWrapper.getCustomSqlSegment());
        return sysOrganMessageMapper.selectMyPage(page, queryWrapper);
    }
}